package com.gaohe.controller;

import com.alibaba.fastjson.JSON;
import com.gaohe.pojo.Goods;
import com.gaohe.service.GoodsService;
import com.gaohe.vo.GoodsVo;
import com.gaohe.vo.InputVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/goods/*")
public class GoodsServlet extends BaseServlet {
    public void search(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        BufferedReader reader = req.getReader();
        String str = reader.readLine();
        InputVo inputVo = JSON.parseObject(str, InputVo.class);

        GoodsService goodsService = new GoodsService();

        PageHelper.startPage(inputVo.getCurrentPage4(),inputVo.getPageSize());
        List<Goods> list = goodsService.findByName(inputVo.getData());//查到的goods
        PageInfo<Goods> page = new PageInfo<>(list);

        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        if (list.isEmpty()){
//            暂无为数据
            writer.write("none");
        }else {
            String jsonString = JSON.toJSONString(page);//转为josn
            writer.write(jsonString);
        }
    }
    public void page(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        String pageNum = req.getParameter("pageNum");
        String pageSize = req.getParameter("pageSize");
        int PageNum = Integer.parseInt(pageNum);
        int PageSize = Integer.parseInt(pageSize);
        GoodsService goodsService = new GoodsService();
        PageInfo<Goods> page = goodsService.page(PageNum, PageSize);
        String toJSONString = JSON.toJSONString(page);
        resp.setContentType("text/json;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.write(toJSONString);
    }
    public void findCon(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        BufferedReader reader = req.getReader();
        String s = reader.readLine();
        GoodsVo goodsVo = JSON.parseObject(s, GoodsVo.class);

        GoodsService goodsService = new GoodsService();

        PageHelper.startPage(goodsVo.getCurrentPage4(),goodsVo.getPageSize());
        List<Goods> list = goodsService.findCon(goodsVo);
        PageInfo<Goods> page = new PageInfo<>(list);

        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        if (list.isEmpty()){
            writer.write("none");
        }else {
            String jsonString = JSON.toJSONString(page);//转为json
            writer.write(jsonString);
        }
    }
    public void add(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        BufferedReader reader = req.getReader();
        String s = reader.readLine();
        Goods goods = JSON.parseObject(s, Goods.class);
        GoodsService goodsService = new GoodsService();
        int add = goodsService.add(goods);
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        if (add == 0){
            writer.write("failed");
        }else {
            writer.write("succeed");
        }
    }
    public void getById(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        int Id = Integer.parseInt(id);
        GoodsService goodsService = new GoodsService();
        Goods goods = goodsService.getById(Id);
        String jsonString = JSON.toJSONString(goods);
        resp.setContentType("text/json;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        writer.write(jsonString);
    }
    public void modById(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        BufferedReader reader = req.getReader();
        String s = reader.readLine();
        Goods goods = JSON.parseObject(s, Goods.class);
        GoodsService goodsService = new GoodsService();
        int update = goodsService.updateById(goods);
        resp.setContentType("text/json;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        if (update == 0){
            writer.write("failed");
        }else {
            writer.write("succeed");
        }
    }
    public void del(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        String str = req.getParameter("id");
        int id = Integer.parseInt(str);
        GoodsService goodsService = new GoodsService();
        int del = goodsService.delById(id);
        resp.setContentType("text/json;charset=UTF-8");
        PrintWriter writer = resp.getWriter();
        if (del == 0){
            writer.write("failed");
        }else {
            writer.write("succeed");
        }
    }
}
